import { useState } from 'react';
import api from '../common/api';

export const listInitState = {
    loading: false,
    error: null,
    list: [],
    total: 0,
    offset: 0,
    limit: 10
};

export function getWhiteList(setWhiteDate, conditions, errorCb) {
    // loading
    setWhiteDate({ ...listInitState, loading: true });
    // fetch data
    const result = api({
        method: 'GET',
        url: 'controller/whitelistAction/getUsers.do',
        params: conditions
    })
    .then(data => setWhiteDate({
        ...listInitState,
        locading: false,
        ...data
    }))
    .catch(errorCb)
    return result;
}

export function deleteWhite(_, id, cb, errorCb) {
    const result = api({
        method: 'GET',
        url: 'controller/whitelistAction/deleteUser.do',
        params: { id }
    })
    .then(cb)
    .catch(errorCb);
    return result;
}

export function getAegis(id, cb) {
    const result = api({
        method: 'GET',
        url: 'controller/whitelistAction/getAegisList.do',
        params: { id }
    })
    .then(cb)
    .catch(console.log);
    return result;
}

export function getWhiteDetail(id, cb, errCb) {
    const result = api({
        method: 'GET',
        url: 'controller/whitelistAction/getUsers.do',
        params: { id }
    })
    .then(res => cb(res.data[0]))
    .catch(errCb)
    return result;
}

export function insertWhite(params, cb, errorCb) {
    const result = api({
        method: 'GET',
        url: 'controller/whitelistAction/addUser.do',
        params
    })
    .then(cb)
    .catch(errorCb);
    return result;
}

export function updateWhite(params, cb, errorCb) {
    const result = api({
        method: 'GET',
        url: 'controller/whitelistAction/updateUser.do',
        params
    })
    .then(cb)
    .catch(errorCb);
    return result;
}
